<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\admin\controller\User;
use \think\Db;
use \think\Cookie;
use app\admin\model\Comment as C;
use app\admin\model\Member as MemberModel;
use app\admin\model\Order as OrderModel;
class Comment extends User
{

    /**
     * 获取小数点位数
     */

    /**
     * 列表首页
     */
    public function index()
    {
        $model=new C();

        $where='';
        $post= request()->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post=Cookie::get('postlist');
        }else{
            //把条件写入缓存
            Cookie::set('postlist',$post);
        }
        if (isset($post['comment_id']) and $post['comment_id'] > 0) {
            $where['comment_id'] = $post['comment_id'];
        }
        if (isset($post['comment_status']) and $post['comment_status'] > 0) {
            $where['comment_status'] = $post['comment_status'];
        }
        $da=  $model->where($where)->order('comment_id desc')->paginate();
        $this->assign('lie',$da);
        $this->assign('wherelist',$post);
        return $this->fetch();

    }


    public function del(){
        $model=new C();
        $id= input('id');


        $data= $model->del($id);

        if ($data==true){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    public function review(){

        $model=new C();
        $id= request()->request();


        $data= $model->review($id);

        if ($data==true){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }




    //添加虚拟用户评论
    public function virtual_addcomment()
    {
        $model = new MemberModel();
        //是新增操作
        if ($this->request->isPost()) {
            //是提交操作
            $post = $this->request->post();

            //****************************************************************
            //by suley
            //自动补全资料
            $post['member_type_id'] = 1;//会员等级为1     普通会员
            $post['member_email'] = "kbx_".time()."@test.com";
            $post['creattime'] = time();
            $post['updatetime'] = time();
            $post['is_test'] = 1;//是否为虚拟用户
            //*******************************************************
            //验证昵称是否存在
            $nickname = $model->where('member_nickname', $post['member_nickname'])->select();
            if (!empty($nickname)) {
                return $this->error('提交失败：该昵称已被占用');
            }
            $exists_goods = db('goods')->where('goods_id',$post['goods_id'])->find();
            if (empty($exists_goods)) {
                return $this->error('提交失败：找不到该商品');
            }
            if (false == $model->allowField(true)->save($post)) {
                return $this->error('添加会员失败');
            } else {
                $member_id = $model->getLastInsID();//插入的虚拟用户ID
                $image_arr = explode(",",$post['other_images']);
                $Img = [];
                foreach ($image_arr as $v) {
                    if($v!==''){
                        $Img[] = array("src"=>$v);
                    }
                }
                $comment_creattime = strtotime($post['comment_time']);

                $result = db('comment')->insert([
                    'content'=>$post['content'],
                    'comment_img'=>json_encode($Img),
                    'member_id'=>$member_id,
                    'goods_id'=>$post['goods_id'],
                    'add_integral'=>0,
                    'creattime'=>$comment_creattime
                ]);
                return $this->success('评论成功');
            }
        } else {
            //非提交操作
            return $this->fetch();
        }
    }


    //增加虚拟订单销量
    public function virtual_order_num()
    {
        if ($this->request->isPost()) {
            //是提交操作
            $post = $this->request->post();
            $exists_goods = db('goods')->where('goods_id',$post['goods_id'])->find();
            if (empty($exists_goods)) {
                return $this->error('提交失败：找不到该商品');
            }
            $num = intval($post['sale_num']);//增加订单数
            $num = $num + $exists_goods['goodssales'];
            $res=db('goods')->where('goods_id',$post['goods_id'])->update(['goodssales'=>$num]);
                if ($res) {
                    return $this->success('销量增加成功');
                }else{
                    return $this->error('销量增加失败');
                }
        } else {
            //非提交操作
            return $this->fetch();
        }
    }


}
